package com.hsnn.medstgmini.pro.plan.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hsnn.medstgmini.base.dict.model.DicArea;
import com.hsnn.medstgmini.base.dict.model.DicDrugform;
import com.hsnn.medstgmini.base.dict.model.DicHosptype;
import com.hsnn.medstgmini.base.std.model.HospInfo;
import com.hsnn.medstgmini.base.std.model.StdCompinfo;
import com.hsnn.medstgmini.base.std.model.StdDrugInfo;
import com.hsnn.medstgmini.pro.plan.dao.BidHospplanDao;
import com.hsnn.medstgmini.pro.plan.model.BidHospplan;
import com.hsnn.medstgmini.pro.plan.model.ProMethod;
import com.hsnn.medstgmini.pro.plan.model.ProjcetType;
import com.hsnn.medstgmini.pro.plan.model.form.BidCollectByHospForm;
import com.hsnn.medstgmini.pro.plan.model.form.BidProjByDrugForm;
import com.hsnn.medstgmini.pro.plan.service.BidHospplanManager;
import com.hsnn.medstgmini.pro.project.model.BidProject;
import com.hsnn.medstgmini.util.DateUtil;
import com.hsnn.medstgmini.util.Pagination;
@Service("bidHospplanManager")
public class BidHospplanManagerImpl implements BidHospplanManager {
	@Autowired
	private BidHospplanDao bidHospplanDao;
	/**
	 * 
	 *@category 获取当前医疗机构申报计划
	 *@author 邱磊
	 *@date 2015年6月12日 下午3:33:22
	 *@param page
	 *@return
	 */
	@SuppressWarnings("unchecked")
	public Pagination getDataList(Pagination page){
		List<Map<String,Object>> list = (List<Map<String, Object>>) bidHospplanDao.getDataList(page).getRows();
		for(Map<String,Object> data : list){
			Object projCat = data.get("projCat");
			data.put("projCat", projCat==null?"":StringUtils.isBlank(projCat.toString())?"":ProjcetType.getName(Integer.parseInt(projCat.toString())));
			data.put("starttime", DateUtil.date((Timestamp)data.get("starttime")));
			data.put("endtime", DateUtil.date((Timestamp)data.get("endtime")));
		}
		page.setRows(list);
		return page;
	}
	/**
	 * 
	 *@category 查询当前医疗机构未申报药品
	 *@author 邱磊
	 *@date 2015年6月12日 下午4:21:53
	 *@param page
	 *@return
	 */
	public Pagination getDrugDataList(Pagination page){
		return bidHospplanDao.getDrugDataList(page);
	}
	@Transactional
	public int batchAddDrugData(List<Map<String,String>> list){
		return bidHospplanDao.batchAddDrugData(list);
	}
	
	
	public List<BidProjByDrugForm> getBidHospplanList(Pagination page, BidHospplan bidHospplan, BidProject bidProject, StdDrugInfo stdDrugInfo, HospInfo hospInfo, DicDrugform dicDrugform, StdCompinfo stdCompinfo) {
		List<Object> objList = bidHospplanDao.getBidHospplanList(page, bidHospplan, bidProject, stdDrugInfo, hospInfo, dicDrugform, stdCompinfo);
		if (null != objList && objList.size() > 0) { 
			List<BidProjByDrugForm> flist = new ArrayList<BidProjByDrugForm>();
			for (int i = 0; i < objList.size(); i++){
				Object[] obj = (Object[]) objList.get(i);
				BidProjByDrugForm form = new BidProjByDrugForm();
				form.setProjId(obj[0]==null?"":obj[0].toString());
				form.setDrugId(obj[1]==null?"":obj[1].toString());
				form.setDrugCode(obj[2]==null?"":obj[2].toString());
				form.setDrugName(obj[3]==null?"":obj[3].toString());
				form.setFormName(obj[4]==null?"":obj[4].toString());
				form.setSpecName(obj[5]==null?"":obj[5].toString());
				form.setFactor(obj[6]==null?"":obj[6].toString());
				form.setUnit(obj[7]==null?"":obj[7].toString());
				form.setMaterialName(obj[8]==null?"":obj[8].toString());
				form.setCompName(obj[9]==null?"":obj[9].toString());
				form.setProdCompId(obj[10]==null?"":obj[10].toString());
				form.setHospCount(obj[11]==null?"":obj[11].toString());
				
				flist.add(form);
			}
			return flist;
		}
		return null;
	}
	
	public List<BidProjByDrugForm> getBidPlanListByDrug(Pagination page, BidHospplan bidHospplan, HospInfo HospInfo, DicArea dicArea, DicHosptype dicHosptype) {
		List<Object> objList = bidHospplanDao.getBidPlanListByDrug(page, bidHospplan, HospInfo, dicArea, dicHosptype);
		if (objList != null && objList.size() > 0) {
			List<BidProjByDrugForm> flist = new ArrayList<BidProjByDrugForm>();
			for (int i = 0; i < objList.size(); i++) {
				Object[] obj = (Object[]) objList.get(i);
				BidProjByDrugForm form = new BidProjByDrugForm();
				form.setHospCode(obj[0]==null?"":obj[0].toString());
				form.setHospName(obj[1]==null?"":obj[1].toString());
				form.setArFullName(obj[2]==null?"":obj[2].toString());
				form.setHospClassify(obj[3]==null?null:Integer.valueOf(obj[3].toString()));
				form.setHospType(obj[4]==null?"":obj[4].toString());
				form.setLasyProAmt(obj[5]==null?"":obj[5].toString());
				form.setPlanProAmt(obj[6]==null?"":obj[6].toString());
				form.setProMethod(obj[7]==null?"":StringUtils.isBlank(obj[7].toString())?"":ProjcetType.getName(Integer.parseInt(obj[7].toString())));
				form.setQualityRequires(obj[8]==null?"":obj[8].toString());
				form.setProjId(obj[9]==null?"":obj[9].toString());
				form.setAreaId(obj[10]==null?"":obj[10].toString());
				form.setHospId(obj[11]==null?"":obj[11].toString());
				flist.add(form);
			}
			return flist;
		}
		return null;
	}
	
	

	/**
	 * @category 获取医疗机构采购计划的表数据
	 * @author 韩守松
	 * @date   2015年6月16日
	 * @param  @param bidHospplan
	 * @param  @return
	 */
	public List<BidHospplan> getBidHospplanList(BidHospplan bidHospplan) {
		return bidHospplanDao.getBidHospplanList(bidHospplan);
	}
	
	
	
	/**
	 * 
	 *@category  采购计划按医疗机构汇总
	 *@author 言科
	 *@date 2015年6月18日
	 *List<Object>
	 */
	public List<BidCollectByHospForm> getBidCollectByHospList(Pagination page,BidHospplan bidHospplan, BidProject bidProject, HospInfo hospInfo,
			DicHosptype dicHosptype, DicArea dicArea) {
		List<Object> objList = bidHospplanDao.getBidCollectByHospList(page, bidHospplan, bidProject, hospInfo, dicHosptype, dicArea);
		if (objList != null && objList.size() > 0) {
			List<BidCollectByHospForm> flist = new ArrayList<BidCollectByHospForm>();
			for (int i = 0; i < objList.size(); i++) {
				Object[] obj = (Object[]) objList.get(i);
				BidCollectByHospForm form = new BidCollectByHospForm();
				form.setProjId(obj[0]==null?"":obj[0].toString());
				form.setHospId(obj[1]==null?"":obj[1].toString());
				form.setHospCode(obj[2]==null?"":obj[2].toString());
				form.setHospName(obj[3]==null?"":obj[3].toString());
				form.setArFullName(obj[4]==null?"":obj[4].toString());
				form.setHospClassifyName(obj[5]==null?"":obj[5].toString());
				form.setHospType(obj[6]==null?"":obj[6].toString());
				form.setDrugCount(obj[7]==null?"":obj[7].toString());
				form.setDrugId(obj[8]==null?"":obj[8].toString());
				flist.add(form);
			}
			return flist;
		}
		return null;
	}
	
	/**
	 * 
	 *@category  采购计划按医疗机构汇总详情
	 *@author 言科
	 *@date 2015年6月18日
	 *List<Object>
	 */
	public List<BidCollectByHospForm> getBidCollectDetailsByHosp(Pagination page,BidHospplan bidHospplan, BidProject bidProject,
			StdDrugInfo stdDrugInfo, HospInfo hospInfo,DicDrugform dicDrugform, StdCompinfo stdCompinfo) {
		List<Object> objList = bidHospplanDao.getBidCollectDetailsByHosp(page, bidHospplan, bidProject, stdDrugInfo, hospInfo, dicDrugform, stdCompinfo);
		if (objList != null && objList.size() > 0) {
			List<BidCollectByHospForm> flist = new ArrayList<BidCollectByHospForm>();
			for (int i = 0; i < objList.size(); i++) {
				Object[] obj = (Object[]) objList.get(i);
				BidCollectByHospForm form = new BidCollectByHospForm();
				form.setDrugCode(obj[0]==null?"":obj[0].toString());
				form.setDrugName(obj[1]==null?"":obj[1].toString());
				form.setFormName(obj[2]==null?"":obj[2].toString());
				form.setSpecName(obj[3]==null?"":obj[3].toString());
				form.setFactor(obj[4]==null?"":obj[4].toString());
				form.setUnit(obj[5]==null?"":obj[5].toString());
				form.setMaterialName(obj[6]==null?"":obj[6].toString());
				form.setCompName(obj[7]==null?"":obj[7].toString());
				form.setLasyProAmt(obj[8]==null?"":obj[8].toString());
				form.setPlanProAmt(obj[9]==null?"":obj[9].toString());
				form.setProMethod(obj[10]==null?"":StringUtils.isBlank(obj[10].toString())?"":ProMethod.getName(Integer.parseInt(obj[10].toString())));
				form.setQualityRequires(obj[11]==null?"":obj[11].toString());
				form.setProjId(obj[12]==null?"":obj[12].toString());
				form.setHospId(obj[13]==null?"":obj[13].toString());
				form.setCompCode(obj[14]==null?"":obj[14].toString());
				flist.add(form);
			}
			return flist;
		}
		return null;
	}
		

	@SuppressWarnings("unchecked")
	public Pagination gotDrugDataList(Pagination page) {
		List<Map<String,Object>> list = (List<Map<String, Object>>) bidHospplanDao.gotDrugDataList(page).getRows();
		for(Map<String,Object> data : list){
			Object proMethod = data.get("proMethod");
			data.put("proMethod", proMethod==null?"":StringUtils.isBlank(proMethod.toString())?"":ProMethod.getName(Integer.parseInt(proMethod.toString())));
		}
		page.setRows(list);
		return page;
	}
	
	/**
	 * 
	 *@category  采购计划按地区汇总
	 *@author wangbing
	 *@date 2015年6月18日
	 *List<Object>
	 */
	@Override
	public Pagination getBidCollectByAreaList(Pagination page){
		return bidHospplanDao.getBidCollectByAreaList(page);
	}
	
	/**
	 * 
	 *@category  采购计划按地区汇总详情（根据id查询）
	 *@author wangbing
	 *@date 2015年6月18日
	 *List<Object>
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pagination getBidCollectDetailsByArea(Pagination page){
		List<Map<String,Object>> list = (List<Map<String, Object>>) bidHospplanDao.getBidCollectDetailsByArea(page).getRows();
		for(Map<String,Object> data : list){
			Object proMethod = data.get("proMethod");
			data.put("proMethod", proMethod==null?"":StringUtils.isBlank(proMethod.toString())?"":ProMethod.getName(Integer.parseInt(proMethod.toString())));
		}
		page.setRows(list);
		return page;
	}
	
	/**
	 * 
	 *@category 删除申报药品
	 *@author wangbing
	 *@date 2015年6月28日 
	 *@param page
	 *@return
	 */
	@Transactional
	@Override
	public int deleteData(List<String> idList){
		return bidHospplanDao.deleteData(idList);
	}
}
